﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PacketRush.Core;
using System.Net;
using System.Threading;

namespace ConnectionTestClient
{
    public class TestProgram
    {
        public volatile bool _Running = false;
        public volatile int portCount = 50001;
        public TestProgram() { }

        public void Start()
        {
            //_Running = true;
            //RunClient();

            Random random = new Random((int)DateTime.Now.Ticks);
            _Running = true;
            while (_Running)
            {
                if (random.Next(1000) == 673)
                {
                    Thread thread = new Thread(RunClient);
                    thread.Start();
                }

                if (Console.KeyAvailable)
                {
                    if (Console.ReadKey().Key == ConsoleKey.Escape)
                    {
                        _Running = false;
                        Console.ReadKey();
                    }
                }
            }
        }

        public void RunClient()
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            ReliableUdpClientNetworkingProvider networkingProvider = new ReliableUdpClientNetworkingProvider(IPAddress.Parse("127.0.0.1"), 0);
            NetClient client = new NetClient(networkingProvider, IPAddress.Parse("127.0.0.1"), 50000);
            client.LocalConnected += new EventHandler<NetEventArgs>(client_Connected);
            client.Error += new EventHandler<NetErrorEventArgs>(client_Error);
            client.Connect();

            while (true && _Running)
            {
                try
                {
                    client.Update();

                    // Randomly disconnect
                    int rand = random.Next(10000);
                    if (rand == 673)
                    {
                        if (client.Connection != null && client.ConnectionState == NetClientConnectionState.Connected)
                        {
                            Console.WriteLine(client.Connection.Id + " Disconnected.");

                            client.Disconnect();
                        }
                    }

                    if (client.ConnectionState == NetClientConnectionState.Connected)
                    {
                    }

                    if (client.ConnectionState == NetClientConnectionState.Disconnected)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("=========================================");
                    Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
                    _Running = false;
                    Console.ReadKey();
                    break;
                }
            }
        }

        void client_Connected(object sender, NetEventArgs e)
        {
            Console.WriteLine(String.Format("{0} Connected.", e.ConnectionId));
        }

        void client_Error(object sender, NetErrorEventArgs e)
        {
            Console.WriteLine("=========================================");
            Console.WriteLine(String.Format("{0}", e.Connection));
            Console.WriteLine(e.SocketErrorCode.ToString());
        }
    }
}
